const ctrlCount = {
  state: {
    ctrlCount: [],
    ctrlSignalCount: {
      tdm: 0,
      aloha: 0
    }
  },
  mutations: {
    setCtrlCount (state, data) {
      state.ctrlCount = data
    },
    pushCtrlCount (state, data) {
      let res = [...state.ctrlCount]
      res.push(data.order_count)
      if (res.length > 10) res.shift()
      state.ctrlCount = res
    },
    setCtrlSignalCount (state, data) {
      state.ctrlSignalCount.tdm = data.tdm_count || 0
      state.ctrlSignalCount.aloha = data.aloha_count || 0
    }
  },
  actions: {
    async getCtrlCount (context) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/ncp/ctrl_count/'
      })
      context.commit('setCtrlCount', response.data)
      context.commit('setCtrlSignalCount', response.data.pop())
    }
  },
  getters: {
    getTdmSignalingCount: (state) => {
      return state.ctrlCount.map(item => {
        return item.tdm_count
      })
    },
    getAlohaSignalingCount: (state) => {
      return state.ctrlCount.map(item => {
        return item.aloha_count
      })
    }
  }
}
export default ctrlCount
